<?php


namespace Api\Event;

// 积分商城活动相关的内部接口

class ActivityEvent extends BaseEvent 
{	

	/** 
	 * [推广获取了次数]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param $object_type 活动的类型; $object_method 操作类型; $object 活动对象数据; $referral 推广者key; $uid 当前用户id
	 * @return null
	 */
	public function userRefer($object_class, $object_method, $object, $referral, $uid = 0)
	{
		$user = event('User')->getByRefer($referral);
		if (!$user) return;
		if (UID && $user['uid'] == UID) return;  // 不能自己给自己增加
		$serv = service('MallActivityRefer');
		$map = [
			'object_type' 	=> $object_class, 
			'object_method' => $object_method,
			'object_id'   	=> (int)$object['id'], 
			'refer_uid'   	=> (int)$user['uid'], 
			'date'			=> date('Y-m-d')
		];
		$has_count = $serv->where($map)->count();
		if ($has_count >= $object['share_max']) return;
		$data = array_merge($map, [
			'uid'		  => (int)$uid, 
			'date'		  => date('Y-m-d', NOW_TIME), 
			'plus_times'  => $object['share_plus'],
 			'creat_time'  => NOW_TIME
		]);
		service('MallActivityRefer')->add($data);
	}

	/** 
	 * [获取用户的次数]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function getUserDailyTimes($object_class, $object, $uid)
	{
		$map = [
			'object_type' 	=> $object_class, 
			'object_id'   	=> (int)$object['id'], 
			'refer_uid'   	=> (int)$uid, 
			'date'			=> date('Y-m-d')
		];
		$res  = service('MallActivityRefer')->where($map)->select();
		$plus = 0;
		if ($res)
		{
			foreach ($res as $key => $value) {
				$plus += $value['plus_times'];
			}
		}

		return $object['user_times'] + $plus ;
	}

	/** 
	 * [验证活动的状态]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function checkStatus($object_class, $object)
	{
		$now_time = date('Y-m-d H:i:s');
		// 如果未发布 或者没有在活动时间中
		if ($object['status'] != 1 ||
			$object['start_time'] > $now_time || 
			( $object['end_time'] && $object['end_time'] < $now_time )
		 )
			return false;
		return true;
	}



}